package api.util;

import api.model.ApiMigration;
import api.service.ApiAuthinfoService;
import m.common.model.Model;
import m.system.RuntimeData;
import m.system.exception.MException;
import m.system.url.UrlMarker;
import m.system.util.HttpRequestUtil;
import m.system.util.JSONMessage;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ApiMigrationInterUtil {
    private final ApiMigration model;
    public ApiMigrationInterUtil(ApiMigration model){
        this.model=model;
    }
    private Map<String,String> getHeader() throws MException {
        Map<String,String> header=new HashMap<>();
        String auth= UrlMarker.getAuth()+" "+ RuntimeData.getService(ApiAuthinfoService.class).getToken(String.valueOf(new Date().getTime()),model.getAuthId(),model.getAuthKey());
        header.put("Authorization",auth);
        return header;
    }
    public JSONMessage verifyModel(String tableClass,List<String> oidList) {
        try {
            JSONMessage json = new JSONMessage();
            json.push("tableClass", tableClass);
            json.push("oidList", oidList);
            HttpRequestUtil util = new HttpRequestUtil();
            String result = util.doJson(model.getServerUrl() + "/action/api/apiMigration/verifyModel", json.toJSONString(), getHeader());
            return new JSONMessage(result);
        }catch (Exception e){
            return JSONMessage.error(e.getMessage());
        }
    }
    public JSONMessage sendModelList(String tableClass, List<? extends Model> modelList) {
        try {
            JSONMessage json = new JSONMessage();
            json.push("tableClass", tableClass);
            json.push("modelList", modelList);
            HttpRequestUtil util = new HttpRequestUtil();
            String result = util.doJson(model.getServerUrl() + "/action/api/apiMigration/saveModelList", json.toJSONString(), getHeader());
            return new JSONMessage(result);
        }catch (Exception e){
            e.printStackTrace();
            return JSONMessage.error(e.getMessage());
        }
    }

}
